//
// UIScrollView+QSExtension.m
// QS_MGKit
//
// Created by Song on 2017/10/31.
// Copyright © 2017年 Song. All rights reserved.
//

#import "UIScrollView+QSExtension.h"

@interface UIScrollView()<UIScrollViewDelegate>

@end

static NSString *didEndDeceleratingBlockKey = @"didEndDeceleratingBlockKey";

@implementation UIScrollView (QSExtension)
/**
 *  ContentSize、ContentOffset
 */
- (CGFloat)contentWidth {
    return self.contentSize.width;
}
- (void)setContentWidth:(CGFloat)width {
    self.contentSize = CGSizeMake(width, self.frame.size.height);
}

- (CGFloat)contentHeight {
    return self.contentSize.height;
}
- (void)setContentHeight:(CGFloat)height {
    self.contentSize = CGSizeMake(self.frame.size.width, height);
}

- (CGFloat)contentOffsetX {
    return self.contentOffset.x;
}
- (void)setContentOffsetX:(CGFloat)x {
    self.contentOffset = CGPointMake(x, self.contentOffset.y);
}

- (CGFloat)contentOffsetY {
    return self.contentOffset.y;
}
- (void)setContentOffsetY:(CGFloat)y {
    self.contentOffset = CGPointMake(self.contentOffset.x, y);
}

/** 滚动方向 */
- (QSScrollDirection)scrollDirection
{
    QSScrollDirection direction;
    
    if ([self.panGestureRecognizer translationInView:self.superview].y > 0.0f) {
        direction = QSScrollDirection_Up;
    } else if ([self.panGestureRecognizer translationInView:self.superview].y < 0.0f) {
        direction = QSScrollDirection_Down;
    } else if ([self.panGestureRecognizer translationInView:self].x < 0.0f) {
        direction = QSScrollDirection_Left;
    } else {
        direction = QSScrollDirection_Right;
    }
    
    return direction;
}

/**
 *  页码
 */
- (NSUInteger)verticalPageIndex
{
    return (self.contentOffset.y + (self.frame.size.height * 0.5f)) / self.frame.size.height;
}

- (NSUInteger)horizontalPageIndex
{
    return (self.contentOffset.x + (self.frame.size.width * 0.5f)) / self.frame.size.width;
}

/**
 *  是否滚动到顶、底、左、右
 */
- (BOOL)isScrolledToTop
{
    return self.contentOffset.y <= [self qs_getTopContentOffset].y;
}

- (BOOL)isScrolledToBottom
{
    return self.contentOffset.y >= [self qs_getBottomContentOffset].y;
}

- (BOOL)isScrolledToLeft
{
    return self.contentOffset.x <= [self qs_getLeftContentOffset].x;
}

- (BOOL)isScrolledToRight
{
    return self.contentOffset.x >= [self qs_getRightContentOffset].x;
}

/**
 *  获取offset
 */
- (CGPoint)qs_getTopContentOffset
{
    return CGPointMake(0.0f, -self.contentInset.top);
}

- (CGPoint)qs_getBottomContentOffset
{
    return CGPointMake(0.0f, self.contentSize.height + self.contentInset.bottom - self.bounds.size.height);
}

- (CGPoint)qs_getLeftContentOffset
{
    return CGPointMake(-self.contentInset.left, 0.0f);
}

- (CGPoint)qs_getRightContentOffset
{
    return CGPointMake(self.contentSize.width + self.contentInset.right - self.bounds.size.width, 0.0f);
}

/**
 *  设置滚动到顶、底、左、右
 */
- (void)qs_scrollToTopAnimated:(BOOL)animated
{
    [self setContentOffset:[self qs_getTopContentOffset] animated:animated];
}

- (void)qs_scrollToBottomAnimated:(BOOL)animated
{
    [self setContentOffset:[self qs_getBottomContentOffset] animated:animated];
}

- (void)qs_scrollToLeftAnimated:(BOOL)animated
{
    [self setContentOffset:[self qs_getLeftContentOffset] animated:animated];
}

- (void)qs_scrollToRightAnimated:(BOOL)animated
{
    [self setContentOffset:[self qs_getRightContentOffset] animated:animated];
}

/**
 *  滚动到指定页码
 */
- (void)qs_scrollToPage:(NSInteger)pageIndex scrollDirection:(QSScrollDirection)scrollDirection andAnimated:(BOOL)animated
{
    CGPoint offset = self.contentOffset;
    
    switch (scrollDirection) {
        case QSScrollDirection_Up:
        case QSScrollDirection_Down: {
            offset.y = pageIndex * self.frame.size.height;
        }
            break;
            
        case QSScrollDirection_Left:
        case QSScrollDirection_Right: {
            offset.x = pageIndex * self.frame.size.width;
        }
            break;
            
        default:
            break;
    }
    
    [self setContentOffset:offset animated:animated];
}

/**
 *  滚动到上一页
 */
- (void)qs_scrollToUpPageWithScrollDirection:(QSScrollDirection)scrollDirection andAnimated:(BOOL)animated
{
    NSInteger pageIndex = 0;
    
    switch (scrollDirection) {
        case QSScrollDirection_Up:
        case QSScrollDirection_Down: {
            pageIndex = self.verticalPageIndex > 0 ? self.verticalPageIndex - 1 : self.verticalPageIndex;
        }
            break;
            
        case QSScrollDirection_Left:
        case QSScrollDirection_Right: {
            pageIndex = self.horizontalPageIndex > 0 ? self.horizontalPageIndex - 1 : self.horizontalPageIndex;
        }
            break;
            
        default:
            break;
    }
    
    [self qs_scrollToPage:pageIndex scrollDirection:scrollDirection andAnimated:animated];
}

/**
 *  滚动到下一页
 */
- (void)qs_scrollToNextPageWithScrollDirection:(QSScrollDirection)scrollDirection andAnimated:(BOOL)animated
{
    NSInteger pageIndex = 0;
    
    switch (scrollDirection) {
        case QSScrollDirection_Up:
        case QSScrollDirection_Down: {
            
            pageIndex = self.verticalPageIndex < (self.contentHeight / self.frame.size.height - 1) ? self.verticalPageIndex + 1 : self.verticalPageIndex;
        }
            break;
            
        case QSScrollDirection_Left:
        case QSScrollDirection_Right: {
            pageIndex = self.horizontalPageIndex < (self.contentWidth / self.frame.size.width - 1) ? self.horizontalPageIndex + 1 : self.horizontalPageIndex;
        }
            break;
            
        default:
            break;
    }
    
    [self qs_scrollToPage:pageIndex scrollDirection:scrollDirection andAnimated:animated];
}

#pragma mark - scrollView完成滚动减速回调
- (void)setDidEndDeceleratingBlock:(void (^)(UIScrollView *))didEndDeceleratingBlock
{
    self.delegate = self.delegate ? self.delegate : self;
    objc_setAssociatedObject(self, &didEndDeceleratingBlockKey, didEndDeceleratingBlock, OBJC_ASSOCIATION_COPY);
}

- (void (^)(UIScrollView *))didEndDeceleratingBlock
{
    return objc_getAssociatedObject(self, &didEndDeceleratingBlockKey);
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (self.didEndDeceleratingBlock) {
        self.didEndDeceleratingBlock(scrollView);
    }
}
@end
